/**
 * 时间转换
 */
export const toDate = time => {
  if (!time)
    return new Date()
  try {
    if (typeof time === 'number')
      return new Date(time)
    if (typeof time === 'string')
      new Date(time.replace(/-/g, '/'))
    return new Date(time)
  } catch (e) {}
  return time
}

/**
 * 添加天数
 * @param {*} time 
 * @param {*} day 
 */
export const dateAdd = (time, day) => {
  var date = toDate(time)
  if (date && day)
    date.setDate(date.getDate() + day)
  return date
}

/**
 * 是否是手机号码
 */
export const isMobile = mobile => {
  return /^1[0-9]{10}$/gi.test(mobile)
}

/**
 * 是否是微信
 */
export function isWeixin() {
  var ua = navigator.userAgent.toLowerCase()
  return /micromessenger/i.test(ua)
}

/**
 * 是否是支付宝
 */
export function isAlipay() {
  var ua = navigator.userAgent.toLowerCase()
  return /alipay/i.test(ua)
}

export const isJson = str => {
  try {
    if (typeof JSON.parse(str) == "object") {
      return true;
    }
  } catch (e) {}
  return false;
}
